package com.loft.comm.util;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.UUID;
import com.loft.comm.MyApp;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Environment;

/**
 * @author heb
 * @class_name Utility.java
 * @create_date 2012-4-3
 * @version 1.0.0
 */
public class Utility {
	private Context _context;

	public Utility(Context context) {
		this._context = context;
	}

	/**
	 * @MethodName: checkNetIsConnected
	 * @Description:�ж��Ƿ��������
	 * @Author: heb
	 * @CreateDate: 2012/04/24
	 */
	public static boolean checkNetIsConnected() {
		try {
			for (Enumeration<?> _enumeration = NetworkInterface
					.getNetworkInterfaces(); _enumeration.hasMoreElements();) {
				NetworkInterface networkInterface = (NetworkInterface) _enumeration
						.nextElement();
				for (Enumeration<?> _enumeration_2 = networkInterface
						.getInetAddresses(); _enumeration_2.hasMoreElements();) {
					InetAddress inetAddress = (InetAddress) _enumeration_2
							.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return true;
					}
				}
			}
		} catch (Exception e) {
			return false;
		}
		return false;
	}

	/**
	 * @MethodName: saveAppGuid
	 * @Description:����Ӧ��GUID
	 * @Author: yangxd
	 * @CreateDate: 2011/10/27
	 */
	private void saveAppGuid(String guid) {
		SharedPreferences sp = _context.getSharedPreferences(
				MyApp.APP_GUID_SHARE_PREFERENCES, 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.putString(MyApp.APP_GUID_SHARE_PREFERENCES, guid);
		editor.commit();
	}

	/**
	 * @MethodName: getAppGuid
	 * @Description:��ȡӦ��GUID
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	private String getAppGuid() {
		return _context.getSharedPreferences(MyApp.APP_GUID_SHARE_PREFERENCES,
				0).getString(MyApp.APP_GUID, "");
	}

	/**
	 * @MethodName: clearAppGuid
	 * @Description:���Ӧ��GUID
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	public void clearAppGuid() {
		SharedPreferences sp = _context.getSharedPreferences(
				MyApp.APP_GUID_SHARE_PREFERENCES, 0);
		SharedPreferences.Editor editor = sp.edit();
		editor.clear();
		editor.commit();
	}

	/**
	 * 
	 * @MethodName: getGUID
	 * @Description:��ȡGUID
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	public String getGUID() {
		String guid = getAppGuid();
		if (guid.equals("")) {
			guid = (UUID.randomUUID()).toString().toUpperCase();
			saveAppGuid(guid);
		}
		return guid;
	}

	public static boolean checkSdCard() {
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED)) {
			return true;
		} else {
			return false;
		}
	}

}
